// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   InformacionDAO.java

package org.fiel.admin.dao;

import java.io.PrintStream;
import java.sql.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.struts.upload.FormFile;
import org.fiel.admin.struts.forms.InformacionForm;
import org.fiel.domain.Informacion;

public class InformacionDAO
{

    public InformacionDAO()
        throws Exception
    {
        conn = null;
        try
        {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/FielDB");
            conn = ds.getConnection();
        }
        catch(Exception e)
        {
            System.err.println("InformacionDAO() Error:\n" + e.getMessage());
            throw new Exception("InformacionDAO() Error:\n" + e.getMessage());
        }
    }

    public void createInformacion(InformacionForm informacion)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("INSERT INTO informacion (NameFile,NameFileE,Titulo,TituloE,Imagen,Nivel,Formato,FechaE,FechaP,Abstract,AbstractE,Costo,CostoE,tiempo,Identipo,Idensubtipo) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            pstmt.setString(1, informacion.getNameFile().getFileName());
            pstmt.setString(2, informacion.getNameFileE().getFileName());
            pstmt.setString(3, informacion.getTitulo());
            pstmt.setString(4, informacion.getTituloE());
            pstmt.setString(5, informacion.getImagen().getFileName());
            pstmt.setString(6, informacion.getNivel());
            pstmt.setString(7, informacion.getFormato());
            pstmt.setString(8, informacion.getFechaE());
            pstmt.setString(9, informacion.getFechaP());
            pstmt.setString(10, informacion.getAbstracto());
            pstmt.setString(11, informacion.getAbstractoE());
            pstmt.setString(12, informacion.getCosto());
            pstmt.setString(13, informacion.getCostoE());
            pstmt.setString(14, informacion.getTiempo());
            pstmt.setString(15, informacion.getIdentipo());
            pstmt.setString(16, informacion.getIdenSubtipo());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("InformacionDAO.createInformacion() Error:\n" + e.getMessage());
            throw new Exception("InformacionDAO.createInformacion() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public List retrieveInformacionByType(String identipo)
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        List informaciones = new ArrayList();
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM informacion WHERE Identipo = ? ORDER BY FechaE DESC");
            pstmt.setString(1, identipo);
            Informacion informacion;
            for(rset = pstmt.executeQuery(); rset.next(); informaciones.add(informacion))
            {
                informacion = new Informacion();
                informacion.setAbstract(rset.getString("Abstract"));
                informacion.setAbstractE(rset.getString("AbstractE"));
                informacion.setCosto(rset.getString("Costo"));
                informacion.setCostoE(rset.getString("CostoE"));
                informacion.setFechaE(rset.getDate("FechaE"));
                informacion.setFechaP(rset.getDate("FechaP"));
                informacion.setFormato(rset.getString("Formato"));
                informacion.setIdenint(Integer.valueOf(rset.getString("Idenint")));
                String idensubtipo = rset.getString("Idensubtipo");
                if(idensubtipo != null)
                    informacion.setIdensubtipo(Integer.valueOf(idensubtipo));
                else
                    informacion.setIdensubtipo(null);
                informacion.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
                informacion.setImagen(rset.getString("Imagen"));
                informacion.setNameFile(rset.getString("NameFile"));
                informacion.setNameFileE(rset.getString("NameFileE"));
                informacion.setNivel(rset.getString("Nivel"));
                informacion.setTiempo(Integer.valueOf(rset.getString("Tiempo")));
                informacion.setTitulo(rset.getString("Titulo"));
                informacion.setTituloE(rset.getString("TituloE"));
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.err.println("InformacionDAO.retrieveInformacionByType() Error:\n" + e.getMessage());
            throw new Exception("InformacionDAO.retrieveInformacionByType() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return informaciones;
    }

    public List retrieveInformacionByTypeAndSubtype(String identipo, String idensubtipo)
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        List informaciones = new ArrayList();
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM informacion WHERE Identipo = ? AND Idensubtipo = ? ORDER BY FechaE DESC");
            pstmt.setString(1, identipo);
            pstmt.setString(2, idensubtipo);
            Informacion informacion;
            for(rset = pstmt.executeQuery(); rset.next(); informaciones.add(informacion))
            {
                informacion = new Informacion();
                informacion.setAbstract(rset.getString("Abstract"));
                informacion.setAbstractE(rset.getString("AbstractE"));
                informacion.setCosto(rset.getString("Costo"));
                informacion.setCostoE(rset.getString("CostoE"));
                informacion.setFechaE(rset.getDate("FechaE"));
                informacion.setFechaP(rset.getDate("FechaP"));
                informacion.setFormato(rset.getString("Formato"));
                informacion.setIdenint(Integer.valueOf(rset.getString("Idenint")));
                informacion.setIdensubtipo(Integer.valueOf(idensubtipo));
                informacion.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
                informacion.setImagen(rset.getString("Imagen"));
                informacion.setNameFile(rset.getString("NameFile"));
                informacion.setNameFileE(rset.getString("NameFileE"));
                informacion.setNivel(rset.getString("Nivel"));
                informacion.setTiempo(Integer.valueOf(rset.getString("Tiempo")));
                informacion.setTitulo(rset.getString("Titulo"));
                informacion.setTituloE(rset.getString("TituloE"));
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.err.println("InformacionDAO.retrieveInformacionByType() Error:\n" + e.getMessage());
            throw new Exception("InformacionDAO.retrieveInformacionByType() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return informaciones;
    }

    public List retrieveInformacionesVencidas()
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        List informaciones = new ArrayList();
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM informacion WHERE nivel <> 0 AND tiempo <> 0");
            for(rset = pstmt.executeQuery(); rset.next();)
            {
                Informacion informacion = new Informacion();
                informacion.setAbstract(rset.getString("Abstract"));
                informacion.setAbstractE(rset.getString("AbstractE"));
                informacion.setCosto(rset.getString("Costo"));
                informacion.setCostoE(rset.getString("CostoE"));
                informacion.setFechaE(rset.getDate("FechaE"));
                informacion.setFechaP(rset.getDate("FechaP"));
                informacion.setFormato(rset.getString("Formato"));
                informacion.setIdenint(Integer.valueOf(rset.getString("Idenint")));
                String idensubtipo = rset.getString("Idensubtipo");
                if(idensubtipo != null)
                    informacion.setIdensubtipo(Integer.valueOf(idensubtipo));
                else
                    informacion.setIdensubtipo(null);
                informacion.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
                informacion.setImagen(rset.getString("Imagen"));
                informacion.setNameFile(rset.getString("NameFile"));
                informacion.setNameFileE(rset.getString("NameFileE"));
                informacion.setNivel(rset.getString("Nivel"));
                informacion.setTiempo(Integer.valueOf(rset.getString("Tiempo")));
                informacion.setTitulo(rset.getString("Titulo"));
                informacion.setTituloE(rset.getString("TituloE"));
                GregorianCalendar fechaVencimiento = null;
                GregorianCalendar hoy = (GregorianCalendar)Calendar.getInstance();
                fechaVencimiento = (GregorianCalendar)Calendar.getInstance();
                fechaVencimiento.setTime(informacion.getFechaE());
                fechaVencimiento.add(2, informacion.getTiempo().intValue());
                if(fechaVencimiento.getTimeInMillis() < hoy.getTimeInMillis())
                    informaciones.add(informacion);
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.err.println("InformacionDAO.retrieveInformacionesVencidas() Error:\n" + e.getMessage());
            throw new Exception("InformacionDAO.retrieveInformacionesVencidas() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return informaciones;
    }

    public Informacion retrieveInformacion(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        Informacion informacion = null;
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM informacion WHERE Idenint = ?");
            pstmt.setString(1, id);
            rset = pstmt.executeQuery();
            if(rset.next())
            {
                informacion = new Informacion();
                informacion.setAbstract(rset.getString("Abstract"));
                informacion.setAbstractE(rset.getString("AbstractE"));
                informacion.setCosto(rset.getString("Costo"));
                informacion.setCostoE(rset.getString("CostoE"));
                informacion.setFechaE(rset.getDate("FechaE"));
                informacion.setFechaP(rset.getDate("FechaP"));
                informacion.setFormato(rset.getString("Formato"));
                informacion.setIdenint(Integer.valueOf(rset.getString("Idenint")));
                String idensubtipo = rset.getString("Idensubtipo");
                if(idensubtipo != null)
                    informacion.setIdensubtipo(Integer.valueOf(idensubtipo));
                else
                    informacion.setIdensubtipo(null);
                informacion.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
                informacion.setImagen(rset.getString("Imagen"));
                informacion.setNameFile(rset.getString("NameFile"));
                informacion.setNameFileE(rset.getString("NameFileE"));
                informacion.setNivel(rset.getString("Nivel"));
                informacion.setTiempo(Integer.valueOf(rset.getString("Tiempo")));
                informacion.setTitulo(rset.getString("Titulo"));
                informacion.setTituloE(rset.getString("TituloE"));
            }
        }
        catch(Exception e)
        {
            System.err.println("InformacionDAO.retrieveInformacion() Error:\n" + e.getMessage());
            throw new Exception("InformacionDAO.retrieveInformacion() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return informacion;
    }

    public void updateInformacion(InformacionForm informacion)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("UPDATE informacion SET NameFile= ?, NameFileE = ?, Titulo = ?, TituloE = ?, Imagen = ?, Nivel = ?, Formato = ?, FechaE = ?, FechaP = ?, Abstract = ?, AbstractE = ?, Costo = ?, CostoE = ?, tiempo = ?, Identipo = ?, Idensubtipo = ? WHERE Idenint = ?");
            if(informacion.getNameFile().getFileSize() > 0)
                pstmt.setString(1, informacion.getNameFile().getFileName());
            else
            if(informacion.getAuxNameFile() != null)
                pstmt.setString(1, informacion.getAuxNameFile());
            else
                pstmt.setString(1, null);
            if(informacion.getNameFileE().getFileSize() > 0)
                pstmt.setString(2, informacion.getNameFileE().getFileName());
            else
            if(informacion.getAuxNameFileE() != null)
                pstmt.setString(2, informacion.getAuxNameFileE());
            else
                pstmt.setString(2, null);
            pstmt.setString(3, informacion.getTitulo());
            pstmt.setString(4, informacion.getTituloE());
            if(informacion.getImagen().getFileSize() > 0)
                pstmt.setString(5, informacion.getImagen().getFileName());
            else
            if(informacion.getAuxImagen() != null)
                pstmt.setString(5, informacion.getAuxImagen());
            else
                pstmt.setString(5, null);
            pstmt.setString(6, informacion.getNivel());
            pstmt.setString(7, informacion.getFormato());
            pstmt.setString(8, informacion.getFechaE());
            pstmt.setString(9, informacion.getFechaP());
            pstmt.setString(10, informacion.getAbstracto());
            pstmt.setString(11, informacion.getAbstractoE());
            pstmt.setString(12, informacion.getCosto());
            pstmt.setString(13, informacion.getCostoE());
            pstmt.setString(14, informacion.getTiempo());
            pstmt.setString(15, informacion.getIdentipo());
            pstmt.setString(16, informacion.getIdenSubtipo());
            pstmt.setString(17, informacion.getIdenint());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("InformacionDAO.updateInformacion() Error:\n" + e.getMessage());
            throw new Exception("InformacionDAO.updateInformacion() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public void deleteInformacion(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("DELETE FROM informacion WHERE Idenint = ?");
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("InformacionDAO.deleteInformacion() Error:\n" + e.getMessage());
            throw new Exception("InformacionDAO.deleteInformacion() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    private Connection conn;
}
